class Solution:
    def longestBeautifulSubstring(self, word: str) -> int:
        vowels = {}
        vowels['a'] = {'a', 'e'}
        vowels['e'] = {'e', 'i'}
        vowels['i'] = {'i', 'o'}
        vowels['o'] = {'o', 'u'}
        vowels['u'] = {'u'}
        stack = []
        res = 0
        for string in word:
            if len(stack) == 0:
                if string == 'a':
                    stack.append(string)
            elif string in vowels[stack[-1]]:
                stack.append(string)
                if string == 'u':
                    res = max(len(stack), res)
            else:
                stack = []
                if string == 'a':
                    stack.append(string)

        return res
